/*
 * Copyright (c) 2023, 2023,yzc.cn All rights reserved.
 *
 */
package cn.yzc.controller;

import cn.yzc.pojo.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>Project: SpringMVCPro - UpLoadController</p>
 * <p>Powered by yzc On 2023-11-02 15:03:18</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */

@RestController
public class UpLoadController {

    private final  String DIR ="/attachment";

    /**
     * 单文件上传
     * @param myFile
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile myFile, HttpServletRequest request) throws IOException {
        //获取新的文件名
       String newFileName= getNewFileName(myFile.getOriginalFilename());
       //目标文件
        String dest = getDestDir(request,"images")+newFileName;
        myFile.transferTo(new File(dest));
        return Result.success(dest);
    }

    /**
     * 多文件上传
     * @param myFiles
     * @param request
     * @return
     */
    @PostMapping("/uploads")
    public Result uploads(@RequestBody List<MultipartFile> myFiles, HttpServletRequest request) {
        myFiles.forEach(myFile->{
            //获取新的文件名
            String newFileName= getNewFileName(myFile.getOriginalFilename());
            //目标文件
            String dest = getDestDir(request,"images")+newFileName;
            try {
                myFile.transferTo(new File(dest));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
       return Result.success("上传成功");
    }

    /**
     * 根据给定的文件生成新的文件名
     * @param fileName
     * @return
     */
    String getNewFileName(String fileName){
        //获取文件的后缀
        int pos = fileName.lastIndexOf(".");
        String sufixx = fileName.substring(pos);
        //新文件名
        String newFileName = System.currentTimeMillis()+sufixx;
        return newFileName;
    }

    /**
     * 生成目标文件存在的目录
     * @param request
     * @param type
     * @return
     */
    String getDestDir(HttpServletRequest request,String type){
        //获取根目录
        String root = request.getServletContext().getRealPath("/");
        //新目录
        String tmp =DIR+"/"+type+"/"+ymd()+"/";
        File file = new File(root+tmp);
        if(!file.exists()){
            //如果文件不存在，创建文件
           file.mkdirs();
        }
        return root+tmp;
    }

    /**
     * 生成当前日期目录名
     * @return
     */
    String ymd(){
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String tmp =date.format(formatter);
        return tmp;
    }
}
